<?php

namespace app\site\controller\delivery;

use app\common\exception\MiException;
use app\model\delivery\PostageTemplateRegion;
use app\model\delivery\PostageTemplateRegionNo;
use app\Request;
use app\site\controller\AController;
use app\site\service\delivery\PostageTemplateRegionConditionService;
use app\site\service\delivery\PostageTemplateRegionNoService;
use app\site\service\delivery\PostageTemplateRegionService;
use app\site\service\delivery\PostageTemplateService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
use think\response\Json;


class PostageTemplateController extends AController
{

    /**
     * @throws DbException|MiException
     */
    public function index(){
        return $this->reqSer->setService(PostageTemplateService::instance())->setPaginate()->getResponse();
    }

    public function add(Request $request): Json
    {
        $post = $this->post();
        $info = $this->save($post);
        return jsonSuccess([
            'info' => $info,
        ]);
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function edit(Request $request){
        $id = $this->param('id');
        if (!$id) throw new MiException("ID不存在");
        if($request->isPost()){

            $post = $this->post();
            $info = $this->save($post);

            return jsonSuccess([
                'info' => $info,
            ]);

        }else{
            return $this->reqSer->setService(PostageTemplateService::instance())->setFind()->getResponse();
        }
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function del(){
        return $this->reqSer->setService(PostageTemplateService::instance())->delete()->getResponse();
    }


    /**
     * 运费模板选择器
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function options(): Json
    {
        $list = PostageTemplateService::instance()->makeQuery()->select();
        return jsonSuccess([
            'options' => $list,
        ]);
    }


    private function save($post){

        // todo: 数据验证

        $info = null;
        Db::transaction(function () use($post, &$info) {
            $ser = PostageTemplateService::instance();
            $id = $post['id']??'';
            if ($id){
                $info = $ser->update($post, ['id' => $id]);
            }else{
                $info = $ser->create($post);
            }
            // 区域运费
            $regions = $post['regions'];
            // 删除旧的
            PostageTemplateRegionService::instance()->delete();
            foreach ($regions as $v){
                // 一律重新赋值模板id
                $v['tpl_id'] = $info->id;
                unset($v['create_time']);
                unset($v['update_time']);
                // 添加新的
                PostageTemplateRegionService::instance()->create($v);
            }
            // 包邮条件
            $conditions = $post['conditions'];
            // 删除旧的
            PostageTemplateRegionConditionService::instance()->delete();
            foreach ($conditions as $v) {
                // 一律重新赋值模板id
                $v['tpl_id'] = $info->id;
                unset($v['create_time']);
                unset($v['update_time']);
                // 添加新的
                PostageTemplateRegionConditionService::instance()->create($v);
            }
            // 不配送地区
            $regionsNo = $post['regionsNo'];
            // 删除旧的
            PostageTemplateRegionNo::destroy($info->id);
            if ($regionsNo){ // 写入新的
                $regionsNo['tpl_id'] = $info->id;
                unset($regionsNo['create_time']);
                unset($regionsNo['update_time']);
                PostageTemplateRegionNoService::instance()->create($regionsNo);
            }
        });

        return $info;
    }


}
